package com.qiaoyanqing.ximalaya.discovermodule.discover_activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.qiaoyanqing.ximalaya.R;
import com.qiaoyanqing.ximalaya.discovermodule.discover_adapter.DiscoverRecommendSpecialLVadapter;
import com.qiaoyanqing.ximalaya.discovermodule.discover_callback.DiscoverRecommendSpecialService;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.DiscoverSpecialEntity;
import com.qiaoyanqing.ximalaya.url.Urlconfig;
import com.qiaoyanqing.ximalaya.util.CircleTransform;
import com.qiaoyanqing.ximalaya.util.MyLog;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class DiscoverSpecialActivity extends AppCompatActivity {

    private ListView mListView;
    private DiscoverRecommendSpecialLVadapter mLVadapter;
    private List<DiscoverSpecialEntity.ListBean> mDataList = new ArrayList<>();
    //  private List<DiscoverSpecialEntity.InfoBean> mHeadInfo=new ArrayList<>();

    //头布局
    private TextView spetitle, speinfo, speusername;
    private ImageView speusericon;
    //尾布局
    private TextView userfootinfo,userfootmsg;
    private ImageView pecuserfooticon ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_discover_special);
        mListView = (ListView) findViewById(R.id.speciallistview);

        initData();

        initListview();


    }

    private void initListview() {

        View Spheadview = LayoutInflater.from(this).inflate(R.layout.spheadview, mListView, false);

        spetitle = (TextView) Spheadview.findViewById(R.id.spetitle);
        speinfo = (TextView) Spheadview.findViewById(R.id.speinfo);
        speusername = (TextView) Spheadview.findViewById(R.id.speusername);
        speusericon = (ImageView) Spheadview.findViewById(R.id.speusericon);

        View Spfootview=LayoutInflater.from(this).inflate(R.layout.spefootview,mListView,false);

        userfootinfo= (TextView) Spfootview.findViewById(R.id.userfootinfo);
        userfootmsg= (TextView) Spfootview.findViewById(R.id.userfootmsg);
        pecuserfooticon= (ImageView) Spfootview.findViewById(R.id.pecuserfooticon);


        mListView.addHeaderView(Spheadview);
        mListView.addFooterView(Spfootview);
        mLVadapter = new DiscoverRecommendSpecialLVadapter(mDataList, this);
        mListView.setAdapter(mLVadapter);

    }


    private void initData() {

        Intent intent =getIntent();

        int  id= intent.getIntExtra("id",691);

        MyLog.i("flag","------intenteid"+id);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Urlconfig.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        DiscoverRecommendSpecialService specialService = retrofit.create(DiscoverRecommendSpecialService.class);
        specialService.specialdetail(id).enqueue(new Callback<DiscoverSpecialEntity>() {
            @Override
            public void onResponse(Call<DiscoverSpecialEntity> call, Response<DiscoverSpecialEntity> response) {

                mDataList.addAll(response.body().getList());
                //头布局
                spetitle.setText(response.body().getInfo().getTitle());
                speinfo.setText(response.body().getInfo().getIntro());
                speusername.setText(response.body().getInfo().getNickname());
                Picasso.with(DiscoverSpecialActivity.this).load(response.body().getInfo().getSmallLogo()).transform(new CircleTransform()).into(speusericon);
                //尾布局
                userfootinfo.setText(response.body().getInfo().getNickname());
                userfootmsg.setText(response.body().getInfo().getPersonalSignature());
                Picasso.with(DiscoverSpecialActivity.this).load(response.body().getInfo().getSmallLogo()).transform(new CircleTransform()).into(pecuserfooticon);
            }

            @Override
            public void onFailure(Call<DiscoverSpecialEntity> call, Throwable t) {

            }
        });


    }


    public void backclick(View view) {
        finish();
    }
}
